<?php

/**
 * MVM_MALL 网上商店系统  商品显示
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-06-25 $
 * $Id: product.php  www.mvmmall.com$
 * ---------------------------------------------
*/

require_once 'include/common.inc.php';
require_once MVMMALL_ROOT.'./include/category_tree.class.php';

$ps_goid = (int)$action;
$product = $db->get_one("SELECT a.* ,b.category_grant,b.category_id,b.category_name,c.supply_id,c.supply_company FROM `{$tablepre}goods_table` AS a LEFT JOIN  `{$tablepre}category` AS b ON a.goods_category = b.category_id  LEFT JOIN {$tablepre}supply_table AS c ON a.goods_supply_id=c.supply_id WHERE a.uid='$ps_goid'");
//删除没有改商品信息的COOKIE
if (isset($_COOKIE['mvm_history'][$ps_goid])){
	setcookie("mvm_history[$ps_goid][uid]",'', time() - 3600 * 24 * 30);
	setcookie("mvm_history[$ps_goid][title]",'', time() - 3600 * 24 * 30);
	setcookie("mvm_history[$ps_goid][photo]",'', time() - 3600 * 24 * 30);
	setcookie("mvm_history[$ps_goid][price]",'', time() - 3600 * 24 * 30);
}

//权限
if ($m_check_rank < $product['goods_grant']) {
!$m_check_id ?  show_msg('mvm_grant', GetBaseUrl('logging', 'login')) : show_msg('grant_member');
}
//会员价格
$other_price = user_price($product);
//浏览次数处理
if ($ps_goid != $_SESSION['user']['mvm_sess_view']){
    unset($_SESSION['user']['mvm_sess_view']);
    $_SESSION['user']['mvm_sess_view'] = $ps_goid;
    $db->query("UPDATE `{$tablepre}goods_table` SET goods_hit = goods_hit + 1 WHERE uid = '$product[uid]'"); 
}

$mm_mall_title         = $product['goods_name']; //标题
$product['goods_key']  && $mm_keywords    = $product['goods_key']; //商品关键字
$product['goods_advance'] && $mm_description = dhtmlchars($product['goods_advance']);//描述
// 商品HTML 是否 使用
if ($product['use_html'] == '1') {
	$product['conter']    = $product['goods_main'];
	$show_goods_head      = $product['goods_head'];
	$show_goods_foot      = $product['goods_foot'];
} else {
	$product['conter']    = dhtmlchars($product['goods_main']);
	$show_goods_head      = dhtmlchars($product['goods_head']);
	$show_goods_foot      = dhtmlchars($product['goods_foot']);
}
$product['goods_point']   = number_format($product['goods_point'],0);
if ($product['goods_email']) {
    $product['email_url'] = "mailer.php?action=sendemail&ps_rmail=".mango_encode($product['goods_email']);
} else {
    $product['email_url'] = "mailer.php?action=sendemail&ps_rmail=". mango_encode($mm_client_email);
}

// 关联商品
if ($product['goods_add_goods']) {
	$add_goods_sql = false;
    $rt_id       = dhtmlchars($product['goods_add_goods']);
	$result = $db->query("SELECT goods_addoption1,goods_addoption2,goods_addoption3,goods_addoption4,goods_name,goods_sale_price,goods_market_price,goods_file1,uid,goods_kg,goods_code,goods_type FROM `{$tablepre}goods_table` WHERE uid IN($rt_id) ORDER BY uid DESC");
	while ($rt = $db->fetch_array($result)) {
		$other_goods[] = goods_array($rt);
	}
	unset($rt);
}

//商品评论
$statr_one   = 0;
$statr_two   = 0;
$statr_three = 0;
$statr_four  = 0;
$statr_five  = 0;
$star_nums   = 0;
$result = $db->query("SELECT * FROM  `{$tablepre}gcomment_table` WHERE guid='$product[uid]' AND approval_date >'0' ORDER BY uid DESC");
while ($rt = $db->fetch_array($result)) {
	if ($star_nums<$mm_comment_nums){
		$rt['title']           = dhtmlchars($rt['comment_subject']);
		$rt['conter']          = dhtmlchars($rt['comment_body']);
		$rt['name']            = dhtmlchars($rt['comment_name']);
		$rt['date']            = date($mm_date_format, $rt['register_date']);
		$rt['star']            = "<IMG  src='$imgpath/Egg_$rt[comment_star].gif'>";
		$rt['approval_date']   = $rt['approval_date']>1?date($mm_date_format,$rt['approval_date']):'';
		$rt['comment_id']      = $m_class_array[$rt['comment_id']];//显示会员等级
		$rep_sql = $db->query("SELECT * FROM  `{$tablepre}reply_comment` WHERE com_id='$rt[uid]' ORDER BY reply_id DESC");
		while ($rs = $db->fetch_array($rep_sql)) {
			$rs['reply_date']  = date($mm_date_format, $rs['reply_date']);
			$rs['user_id']     = $m_class_array[$rs['user_id']];
			$reply[]           = $rs;
		}
		$rt['reply']           = $reply;
		$comment[]             = $rt;
		unset($reply);
	}
	++$star_nums;//总票数
	switch ($rt['comment_star']){
		case 1: {++$statr_one;break;}
		case 2: {++$statr_two;break;}
		case 3: {++$statr_three;break;}
		case 4: {++$statr_four;break;}
		case 5: {++$statr_five;break;}
		default: break;
	}
}
unset($rt);
$rebate_five  =  sprintf("%01.2f",($statr_five/$star_nums)*100) .'%';
$rebate_four  =  sprintf("%01.2f",($statr_four/$star_nums)*100) .'%';
$rebate_three =  sprintf("%01.2f",($statr_three/$star_nums)*100) .'%';
$rebate_two   =  sprintf("%01.2f",($statr_two/$star_nums)*100) .'%';
$rebate_one   =  sprintf("%01.2f",($statr_one/$star_nums)*100) .'%';

//end

$product['free_delivery']      = $product['free_delivery'] ? "[$lang[freeshipping]]" : '';//免运费
$product['url']                = GetBaseUrl('product', $product['uid']);
$show_addoption1               = $product['goods_addoption1'] ? "<img src=\"$imgpath/good_hot.gif\" border=\"0\">" : '';
$show_addoption2               = $product['goods_addoption2'] ? "<img src=\"$imgpath/good_sale.gif\" border=\"0\">" : '';
$show_addoption3               = $product['goods_addoption3'] ? "<img src=\"$imgpath/good_new.gif\" border=\"0\">" : '';
$show_addoption4               = $product['goods_addoption4'] ? "<img src=\"$imgpath/good_none.gif\" border=\"0\">" : '';
$product['addoption']          = $show_addoption1 . $show_addoption2 . $show_addoption3 . $show_addoption4;
$product['web_url']            = MVMMALL_DIR . '/'.GetBaseUrl('product', $product[uid]) . "&userid=$m_check_uid";
$product['qq_url']             = "http://wpa.qq.com/msgrd?v=1&amp;uin=$mm_client_qq1&amp;site=$mm_mall_name&amp;menu=yes";
$product['img_url']            = "gallery.php?uid=$product[uid]";
$product['title']              = $product['goods_name'];
$product['photo']              = $product['goods_file1'];
$product['supply']             = $product['supply_company'] ? $product['supply_company'] : '--';
$product['goods_market_price'] = currency($product['goods_market_price']);
$product['goods_sale_price']   = currency($product['goods_sale_price']);

//品牌
$mm_brand = $cache->get_cache('brand');
if ($rewrite == 1 && $product['goods_brand']) {
   $product['goods_brand']     = "<a href=\"brand-view-$product[goods_brand].html\" target=\"_blank\">".$mm_brand[$product['goods_brand']].'</a>';
   $product['com_url']         =  "comment-view-$product[uid]-1.html";
} elseif ($product['goods_brand']){
   $product['goods_brand']     = "<a href=\"brand.php?action=view&id=$product[goods_brand]\" target=\"_blank\">".$mm_brand[$product['goods_brand']].'</a>';
   $product['com_url']         =  "comment.php?action=view&id=$product[uid]";
}else {
    $product['goods_brand']    = '--';
    $product['com_url']        =  "comment.php?action=view&id=$product[uid]";
}

/**
 * 记录浏览历史,利用cookie的多维数组记录，
 * 利用商品的id做索引，省掉了每次都写入cookie，加快程序运行速度....
 */
$history_see = array();
if (empty($_COOKIE['mvm_history'][$ps_goid])){
    $history_see=dhtmlchars($_COOKIE['mvm_history']);
    //把最后的一个浏览，移除掉.....
    if(count($history_see)>=$mm_history_number){
        $see_array= array_pop($history_see);
        $unid=$see_array[uid];     //需要移除的数组索引
        setcookie("mvm_history[$unid][uid]","", time() - 3600 * 24 * 30);
        setcookie("mvm_history[$unid][title]","", time() - 3600 * 24 * 30);
        setcookie("mvm_history[$unid][photo]","", time() - 3600 * 24 * 30);
        setcookie("mvm_history[$unid][price]","", time() - 3600 * 24 * 30);
    }
        setcookie("mvm_history[$ps_goid][uid]",$ps_goid, time() + 3600 * 24 * 30);
        setcookie("mvm_history[$ps_goid][title]",$product[title], time() + 3600 * 24 * 30);
        setcookie("mvm_history[$ps_goid][photo]",$product[photo], time() + 3600 * 24 * 30);
        setcookie("mvm_history[$ps_goid][price]",$product[goods_sale_price], time() + 3600 * 24 * 30);
}
/**end**/

//属性价格
$attval               = false;
$attvalue             = false;
$attval_cart          = false;
$attval               = goods_att($product[uid]);
$attvalue             = $attval[1];//一般属性
$attval_cart          = $attval[0]; //属性价格
//商品tag
$goods_tag            = false;
$result               = $db->query("SELECT tag_id, member_id, tag_name, COUNT(tag_id) AS tag_count FROM `{$tablepre}tag` WHERE goods_id='$product[uid]'  GROUP BY tag_name");
while ($rt = $db->fetch_array($result)){
    $tag_name         = urlencode($rt['tag_name']);
    $rt[tag_name]     = "<a href='tag.php?action=view&tag_name=$tag_name' target='_blank'>$rt[tag_name]</a>";
    $goods_tag[]      = $rt;
}
unset($rt);
//end

//文章列表
$sql                   = "SELECT a.uid,a.board_subject,a.headnum,b.goods_id,b.article_id FROM `{$tablepre}bmain` a LEFT JOIN {$tablepre}goods_article b ON a.uid=b.article_id WHERE b.goods_id='$product[uid]' ORDER BY a.headnum ASC";
$result                = $db->query($sql);
while ($rt=$db->fetch_array($result)){
    $rt['subject']     = cut_str($rt['board_subject'],40);
    $rt['title']       = $rt['board_subject'];
    $rt['url']         =   $rewrite==1?"article-freeboard-$rt[uid].html":"article.php?action=freeboard&id=$rt[uid]";
    unset($rt['board_subject']);
    $art_list[]        = $rt;
}


//批发方案
$who_rt                = $db->get_one("SELECT * FROM `{$tablepre}wholesale` WHERE goods_id=' $product[uid]' AND enabled='1'");
$who_rt['rank_list']   = explode(',',$who_rt['rank_list']);
$who_list              = unserialize($who_rt['price']);
if (in_array($mm_group_id,$who_rt['rank_list']) && $who_rt){
	foreach ($who_list as $val){
		$val['price']  = currency($val['price'])."/$lang[entries]";
		$val['title']  = $lang['wholesale'].$val['nums'].$lang['entries'];
		$other_price[] = $val;
	}
}
$tree                 = new tree();
$tree->arr_tree       = $cache->get_cache('tree');
$arr                  = $tree->get_parent($product['goods_category']);
$navigation           = urlcat($arr).$tree->arr_tree[$product['goods_category']][0];//导航
require_once MVMMALL_ROOT . 'header.php';
include_once template($product['goods_skin']);
footer();
